package com.example.lib_car.applyparking

import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.example.lib_car.ParkingEntity
import com.example.lib_common.mvi.BaseViewModel
import com.example.lib_common.mvi.ResponseEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch

class ApplyParkingViewModel : BaseViewModel<ApplyParkingRepository,ApplyParkingState>(){
    override fun createRepo(): ApplyParkingRepository {
        return ApplyParkingRepository()
    }
    var list : ArrayList<ColorEntity> =ArrayList()
    var belong : ArrayList<DepartmentEntity> =ArrayList()
    override fun createState(): ApplyParkingState {
        return ApplyParkingState(ResponseEntity(0,"",list),
            ResponseEntity(0,"",CarEntity()),
            ResponseEntity(0,"",belong),
            ResponseEntity(0,"", AddCarEntity()),
            ResponseEntity(0,"",UpLoadImgEntity())
        )
    }


    fun subParking(map: HashMap<String, Any> ){
        mutableContainer.updateRequestState { copy(
            request=true
        ) }
        viewModelScope.launch (Dispatchers.IO){
            repo.requestSubParking(map).flowOn(Dispatchers.IO)
                .collect{
                mutableContainer.updateRequestState { copy(
                    request=false,
                    complete=true
                ) }
                mutableContainer.updateUiState { copy(
                    entity1=it
                ) }
            }
        }
    }

    fun requestColor(){
        mutableContainer.updateRequestState { copy(
            request=true
        ) }
        viewModelScope.launch (Dispatchers.IO){
            repo.requestColorCar().flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState { copy(
                        request=false,
                        complete=true
                    ) }
                    mutableContainer.updateUiState { copy(
                        entity=it
                    ) }
                }
        }
    }

    fun requestDepartment(map: HashMap<String,Any>){
        mutableContainer.updateRequestState { copy(
            request=true
        ) }
        viewModelScope.launch (Dispatchers.IO){
            repo.requestBelong(map).flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState { copy(
                        request=false,
                        complete=true
                    ) }
                    mutableContainer.updateUiState { copy(
                        entity2=it
                    ) }
                }
        }
    }

    fun requestAddCar(map: HashMap<String,Any>){
        mutableContainer.updateRequestState { copy(
            request=true
        ) }
        viewModelScope.launch (Dispatchers.IO){
            repo.requestAddCar(map).flowOn(Dispatchers.IO)
                .collect{
                    mutableContainer.updateRequestState { copy(
                        request=false,
                        complete=true
                    ) }
                    mutableContainer.updateUiState { copy(
                        entity3=it
                    ) }
                }
        }
    }
    fun requestUpLoadImg(path:  String){
        mutableContainer.updateRequestState { copy(
            request=true
        ) }
        viewModelScope.launch (Dispatchers.IO){
            repo.upLoadImg(path).flowOn(Dispatchers.IO)
                .catch { t:Throwable ->
                    LogUtils.e(t.message)
                }
                .collect{
                    mutableContainer.updateRequestState { copy(
                        request=false,
                        complete=true
                    ) }
                    mutableContainer.updateUiState { copy(
                        entity4=it
                    ) }
                }
        }
    }
}